/*******************************************************************************
 File Name        : ftcpsocket.h
 Summary          : A tcp socket port of FNetProtocol
 Create           : 2023-08-24
 Author           : Schindleren
 Update history   :
 NO.     Author        Date         Content
 001     Schindleren   2023-08-24   Create New
*******************************************************************************/
#ifndef FTCPSOCKET_H
#define FTCPSOCKET_H

#include <QByteArray>
#include <QObject>
#include <QTcpSocket>
#include <QHostAddress>

#include "fnetprotocol.h"

class FTcpSocket :public QObject, public FNetProtocol
{
    Q_OBJECT
public:
    explicit FTcpSocket(QTcpSocket *pSocket = nullptr);
    ~FTcpSocket();

    bool Flush();
    bool WaitForBytesWritten(int msecs = 30000);
    bool WaitForReadyRead(int msecs = 30000);
    qint64 Write(const QByteArray & byteArray);
    void ConnectToHost(const QHostAddress & address, quint16 port, QAbstractSocket::OpenMode openMode = QAbstractSocket::ReadWrite);
    bool WaitForConnected(int msecs = 30000);
    bool WaitForDisconnected(int msecs = 30000);
    void DisconnectFromHost();
    QHostAddress PeerAddress() const;
    QAbstractSocket::SocketState State() const;

private:
    qint32 RecvPackData(QByteArray &bytePack);

signals:
    void StateChanged(QAbstractSocket::SocketState);
    void SigPackData(const QByteArray &byteData);
    void Disconnected();

private slots:
    void SlotReadyRead();

private:
    QTcpSocket      *m_pTcpSocket;
};

#endif // FTCPSOCKET_H
